<template>
  <Money v-model="sVal" v-bind="$attrs" />
</template>
<script>
import { exchangeRate } from '@/api/system/config'
export default {
  props: {
    date: {
      type: [Date, String],
      default: ''
    },
    currency: {
      type: String,
      default: () => ''
    },
    value: {
      type: [Number, String],
      default: '',
      desc: '数值'
    }
  },
  data() {
    return {
      sVal: ''
    }
  },
  watch: {
    date(nVal, oVal) {
      if (nVal && oVal) {
        this.load(true)
      }
    },
    value(val) {
      if (val) {
        this.sVal = val
      }
    },
    sVal: {
      handler(val) {
        if (val) {
          this.$emit('input', val)
          this.$emit('change', val)
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    this.load(false)
  },
  methods: {
    load(flag) {
      if (flag === true) {
        const cur = this.currency
        const date = this.parseTime(this.date)
        if (cur !== '' && date !== '') {
          const row = {
            currency: cur,
            date: date
          }
          exchangeRate(row).then(res => {
            if (res.code === 200) {
              this.sVal = res.data
              this.$emit('input', this.sVal)
              this.$emit('change', this.sVal)
            }
          })
        }
      } else {
        this.sVal = this.value
      }
    }
  }
}
</script>
